package com.academic_system.controller;


import com.academic_system.model.Student;
import com.academic_system.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/student")
@CrossOrigin(origins = "*")

public class LoginController {
    @Autowired
    private StudentService studentService;

	@RequestMapping("/login")
	public void login(String name, String password, HttpServletResponse response, HttpSession session) throws IOException {
		System.out.println("uname:"+name+" pwd:"+password);
		boolean stu_login = studentService.queryStudentByNumAndPwd(name, password);
		boolean te_login = studentService.queryTeacherByNumAndPwd(name,password);
		PrintWriter out = response.getWriter();
		if(stu_login) {
			System.out.println(name+"="+password);
			Student student = studentService.queryDisNameAndPwd(name,password);
			System.out.println("保存当前用户");
			System.out.println(student);
			session.setAttribute("currStu", student);
			session.setAttribute("Test","hahaha");
			out.write("s");
			System.out.println(stu_login);
		}else if(te_login){
			out.write("t");
			System.out.println(te_login);
		}else {
			out.write("f");
			System.out.println("登录失败！");
		}

	}
}
